package com.liran.controller;

import com.liran.mpPojo.ChinaAreaReflection;
import com.liran.mpPojo.OutsideAreaReflection;
import com.liran.param.SourcePlatformReflectionParam;
import com.liran.service.ExcelFileService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: whh
 * @Date: 2024/4/22
 * @Description:
 */
@RestController
@RequestMapping("/excel")
@AllArgsConstructor
public class ExcelFileController {

    @Autowired
    private ExcelFileService excelFileService;

    @PostMapping("/generate")
    public void generateFile(MultipartFile file, HttpServletResponse response) throws IOException {
        try {
            excelFileService.generateExcelByFile(file, response);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            throw e;
        }
    }

    @PostMapping("/config")
    public Boolean config(@RequestBody SourcePlatformReflectionParam param) throws IOException {
        return excelFileService.config(param);
    }

    @PostMapping("/chinaAreaConfig")
    public Boolean chinaAreaConfig(@RequestBody ChinaAreaReflection chinaAreaReflection) throws IOException {
        return excelFileService.chinaAreaConfig(chinaAreaReflection);
    }

    @PostMapping("/outsideAreaConfig")
    public Boolean outsideAreaConfig(@RequestBody OutsideAreaReflection outsideAreaReflection) throws IOException {
        return excelFileService.outsideAreaConfig(outsideAreaReflection);
    }
}
